package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 游记评论接口实现类
 */
@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository travelCommentRepository;
    @Autowired
    private MongoTemplate template;
    
    @Autowired
    private  ITravelCommentService travelCommentService;



    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelComment.setCreateTime(new Date());
        String refId = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(refId)){
            TravelComment comment = this.get(refId);
            travelComment.setRefComment(comment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentRepository.findAll();
    }


}
